//
//  WJAccount.m
//  weibo
//
//  Created by 王俊 on 16/1/2.
//  Copyright © 2016年 王俊. All rights reserved.
//

#import "WJAccount.h"
#import <objc/runtime.h>

/*
 第三方框架：
 JsonModel:将字典转成模型/将模型转成字典
 MJExtension:同上
 */


@implementation WJAccount


+ (instancetype)accountWithDict:(NSDictionary*)dict
{
    WJAccount *account = [[WJAccount alloc]init];
    account.access_token = [dict objectForKey:@"access_token"];
    account.expires_in = [dict objectForKey:@"expires_in"];
    account.remind_in = [dict objectForKey:@"remind_in"];
    account.uid = [dict objectForKey:@"uid"];    
    return account;
}

//将模型写入到文件中时调用
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    
    /*
     [aCoder encodeObject:self.access_token forKey:@"access_token"];
     [aCoder encodeObject:self.expires_in forKey:@"expires_in"];
     [aCoder encodeObject:self.remind_in forKey:@"remind_in"];
     [aCoder encodeObject:self.uid forKey:@"uid"];
     [aCoder encodeObject:self.expires_time forKey:@"expires_time"];
     */
    //利用runtime遍历所有属性归档
    unsigned int count = 0;
    objc_property_t *properties =  class_copyPropertyList([self class], &count);
    for (int i = 0 ; i < count; i++) {
        const char *propertyName = property_getName(properties[i]);
        NSString *name = [NSString stringWithUTF8String:propertyName];
        //WJLog(@"%@",name);
        id value = [self valueForKey:name];
        [aCoder encodeObject:value forKey:name];
    }


    
}
//从文件中读取模型时调用
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super init]) {
        
        /*
         self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
         self.expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
         self.remind_in = [aDecoder decodeObjectForKey:@"remind_in"];
         self.uid = [aDecoder decodeObjectForKey:@"uid"];
         self.expires_time = [aDecoder decodeObjectForKey:@"expires_time"];
         */
    //利用runtime遍历所有属性解归档
        unsigned int count = 0;
        objc_property_t *properties = class_copyPropertyList([self class], &count);
        for (int i = 0; i < count; i++) {
            const char *property = property_getName(properties[i]);
            NSString *name = [NSString stringWithUTF8String:property];
            id value = [aDecoder decodeObjectForKey:name];
            [self setValue:value forKey:name];
        }

    }
    return self;
}


- (BOOL)save
{
    NSString *accountPath = [@"account.data"appendDocumentDir];
    NSDate *now = [NSDate date];
    self.expires_time = [now dateByAddingTimeInterval:[self.expires_in doubleValue]];
    return [NSKeyedArchiver archiveRootObject:self toFile:accountPath];
    
}
+ (instancetype)accountFromSandBox
{
    NSString *accountPath = [@"account.data"appendDocumentDir];
    WJAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:accountPath];
    NSDate *now = [NSDate date];
    if ([now compare:account.expires_time] != NSOrderedAscending) {
        return nil;
    }
    else{
        return account;
    }
    
}


@end
